local extension = Package:new("YS__sumeru")
extension.extensionName = "genshin"

local U = require "packages/utility/utility"

Fk:loadTranslationTable{
     ["YS__sumeru"] = "○神-须弥",
     ["YS"] = "○神"
}

local YS__dl = General(extension, "YS__dl", "Electro", 4, 4, General.Female)
local YS__dlwin = fk.CreateActiveSkill{ name = "YS__dl_win_audio" }
YS__dlwin.package = extension
Fk:addSkill(YS__dlwin)
local YS__modeng = fk.CreateTriggerSkill{
    name = "YS__modeng",
    events = {fk.EventPhaseStart},
    mute = true,
    can_trigger = function(self, event, target, player, data)
      return player:hasSkill(self) and player.phase == Player.Play
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      room:notifySkillInvoked(player, "YS__modeng")
      player:broadcastSkillInvoke("YS__modeng", math.random(1,3))
      local tos = room:askForChoosePlayers(player, table.map(room:getOtherPlayers(player), Util.IdMapper), 1, 1, "#YS__modeng-choose", self.name, false, false)
      if #tos > 0 then
        local availableTargets = table.map(table.filter(room.alive_players, function(p) 
            return p ~= player and p:getMark("@@shouhou") ~= 0
          end), function(p)
            return p.id
          end)
        if #availableTargets > 0 then
          for _, targetId in ipairs(availableTargets) do
            room:setPlayerMark(room:getPlayerById(targetId), "@@shouhou", 0)
          end
        end
        local to = room:getPlayerById(tos[1])
        room:setPlayerMark(to, "@@shouhou", 1)
        room:setPlayerMark(player, "shouhou", tos[1])
      end
      local b = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1, "视为对一名角色造成一点伤害", self.name, true, false)
      if b then
      room:damage{
        from = player,
        to = room:getPlayerById(b[1]),
        damage = 1,
        skillName = self.name,
        isVirtualDMG = true,
      }
      end
    end,
  }
  local shouhou = fk.CreateTriggerSkill{
    name = "#shouhou",
    anim_type = "support",
    events = {fk.Damaged, fk.HpRecover, fk.AfterCardsMove},
    can_trigger = function(self, event, target, player, data)
      if event == fk.AfterCardsMove then
        if player:hasSkill(self) and player:getMark("shouhou") ~= 0 then
            for _, move in ipairs(data) do
              if #move.moveInfo > 0 and ((move.from == player.id and move.to ~= player.id and move.moveReason == fk.ReasonDiscard) or
                (move.to == player.id and move.toArea == Card.PlayerHand)) then
                return true
              end
            end
        end
      else
        return player:hasSkill(self) and not target.dead and target:getMark("shouhou") ~= 0
      end
    end,
    on_cost = function(self, event, target, player, data)
      return true
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local m = player:getMark("shouhou")
      local to = room:getPlayerById(m)
      if event == fk.AfterCardsMove then
        for _, move in ipairs(data) do
        if move.to == player.id and move.toArea == Card.PlayerHand then
        local a = room:askForSkillInvoke(player, self.name, nil, "#shouhou-draw::"..to.id)
          if a then
            room:notifySkillInvoked(player, "YS__modeng")
            player:broadcastSkillInvoke("YS__modeng", 7)
            to:drawCards(1)
          end
        else
        local a = room:askForSkillInvoke(player, self.name, nil, "#shouhou-discard::"..to.id)
          if a then
            local cards = table.filter(to:getCardIds("eh"), function(id) return not player:prohibitDiscard(Fk:getCardById(id)) end)
            if #cards > 0 then
                room:notifySkillInvoked(player, "YS__modeng")
                player:broadcastSkillInvoke("YS__modeng", math.random(5,6))
                room:throwCard(table.random(cards, 1), self.name, to, player)
            end
          end
        end
        end
      else
        if event == fk.HpRecover then
        local a = room:askForSkillInvoke(target, self.name, nil, "#shouhou-recover::"..to.id)
        if a then
        room:notifySkillInvoked(player, "YS__modeng")
        player:broadcastSkillInvoke("YS__modeng", 4)
        room:recover({
        who = to,
        num = 1,
        recoverBy = player,
        skillName = self.name
        })
        end
        else
        local a = room:askForSkillInvoke(target, self.name, nil, "#shouhou-damage::"..to.id)
        if a then
        room:notifySkillInvoked(player, "YS__modeng")
        player:broadcastSkillInvoke("YS__modeng", math.random(1,3))
        room:damage{
        from = player,
        to = to,
        damage = 1,
        damageType = fk.ThunderDamage,
        skillName = self.name,
        }
        end
        end
      end
    end,
  }
YS__dl:addSkill(YS__modeng)
YS__modeng:addRelatedSkill(shouhou)
Fk:loadTranslationTable{
    ["YS__dl"] = "多莉",
    ["YS__modeng"] = "魔灯",
    [":YS__modeng"] = "出牌阶段开始时，你可以选择一名角色令其获得“灯中幽精”并视为售后目标(场上至多存在一个售后目标)，然后你可以视为对一名角色造成一点伤害。<br/>当你受到伤害后，你可以对售后目标造成一点雷电伤害；<br/>当你回复体力后，你可以令售后目标回复一点体力；<br/>当你的牌被弃置后，你可以令售后目标随机弃置一张牌；<br/>当你获得牌后，你可以令售后目标摸一张牌。",
    ["#shouhou"] = "售后",
    ["@@shouhou"] = "<font color='#8600FF'>灯中幽精</font>",
    ["#YS__modeng-choose"] = "魔灯:选择一名其他角色为其“售后”",
    ["#shouhou-draw"] = "售后:令%dest摸一张牌",
    ["#shouhou-discard"] = "售后:令%dest随机弃置一张牌",
    ["#shouhou-damage"] = "售后:对%dest造成一点雷电伤害",
    ["#shouhou-recover"] = "售后:令%dest回复一点体力",
    ["$YS__modeng1"] = "大显身手时间！",
    ["$YS__modeng2"] = "看我的！",
    ["$YS__modeng3"] = "魔灯显灵！",
    ["$YS__modeng4"] = "救命之恩，记得给钱！",
    ["$YS__modeng5"] = "留下摩拉来！",
    ["$YS__modeng6"] = "一手交钱，一手交货。",
    ["$YS__modeng7"] = "多多益善，多多益善。",
    ["$YS__dl_win_audio"] = "我的摩拉是我的，你的摩拉还是我的，嘿嘿。",
    ["~YS__dl"] = "啊救命啊，抢劫啦！",
}

local YS__aehs = General(extension, "YS__aehs", "Dendro", 3, 3, General.Male)
local YS__aehswin = fk.CreateActiveSkill{ name = "YS__aehs_win_audio" }
YS__aehswin.package = extension
Fk:addSkill(YS__aehswin)
local YS__gongxiang = fk.CreateTriggerSkill{
  name = "YS__gongxiang",
  anim_type = "drawcard",
  mute = true,
  events = {fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    local cardnum = player:getHandcardNum()
    local cardpoint = data.card.number
    return target == player and player:hasSkill(self) and cardpoint > 0 and cardnum%2 == cardpoint%2
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, "YS__gongxiang")
    player:broadcastSkillInvoke("YS__gongxiang", math.random(1,3))
    player:drawCards(1, self.name)
  end,
}
local YS__gongxiang_buff = fk.CreateTargetModSkill{
  name = "#YS__gongxiang_buff",
  mute = true,
  residue_func = function(self, player, skill, scope, card)
    local cardnum = player:getHandcardNum()
    local cardpoint = card.number
    return (cardnum%2 == cardpoint%2 and card and card.number > 0) and 998 or 0
  end,
}
YS__aehs:addSkill(YS__gongxiang)
YS__gongxiang:addRelatedSkill(YS__gongxiang_buff)
YS__aehs:addSkill("ex__zhiheng")

Fk:loadTranslationTable{
  ["YS__aehs"] = "艾尔海森",
  ["YS__gongxiang"] = "共相",
  [":YS__gongxiang"] = "当你使用或打出牌时若：①此牌点数与你的手牌数奇偶性不同，你摸一张牌；②此牌点数与你的手牌数奇偶性相同，无次数限制。",
  ["$ex__zhiheng_YS__aehs1"] = "分光",
  ["$ex__zhiheng_YS__aehs2"] = "理论的推演",
}

local YS__nl = General(extension, "YS__nl", "Hydro", 3, 3, General.Female)
local YS__nlwin = fk.CreateActiveSkill{ name = "YS__nl_win_audio" }
YS__nlwin.package = extension
Fk:addSkill(YS__nlwin)
local YS__fengkui = fk.CreateActiveSkill{
  name = "YS__fengkui",
  anim_type = "drawcard",
  card_num = 0,
  target_num = 0,
  can_use = function(self, player)
    local cards = table.clone(player:getCardIds("h"))
    local n = 0
    for _, id in ipairs(cards) do
      if Fk:getCardById(id, true).name == "YS__Seed" then
        n = n + 1
      end
    end
    return player:usedSkillTimes(self.name, Player.HistoryPhase) < 1 + player:getMark("YS__nljianwu-turn") and n == 0
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local ids = {}
    local cardSuit = {Card.Spade, Card.Heart}
    local n = 1
    local cards = table.clone(player:getCardIds("h"))
    local colors = {}
    for _, id in ipairs(cards) do
      table.insertIfNeed(colors, Fk:getCardById(id):getColorString())
    end
    if #colors < 2 then
      n = 2
    end
    while n > 0 do
    local card = room:printCard("YS__Seed", cardSuit[math.random(1,2)], 1)
    table.insert(ids, card.id)
    n = n - 1
    end
    room:moveCards({
      ids = ids,
      to = player.id,
      toArea = Card.PlayerHand,
      moveReason = fk.ReasonPrey,
      proposer = player.id,
      skillName = self.name,
      moveVisible = true,
    })
  end,
}
local YS__fulian = fk.CreateTriggerSkill{
  name = "YS__fulian",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.HpLost},
  can_trigger = function(self, event, target, player, data)
      return player:hasSkill(self)
  end,
  on_trigger = function(self, event, target, player, data)
    for i = 1, data.num do
      self:doCost(event, target, player, data)
    end
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(1)
  end,
}
local YS__fulianRecover = fk.CreateTriggerSkill{
  name = "#YS__fulianRecover",
  anim_type = "support",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId, true).name == "YS__Seed" then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "浮莲：回复一点体力")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = self.name
        })
  end,
}
local YS__jianwu = fk.CreateTriggerSkill{
  name = "YS__jianwu",
  mute = true,
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if event == fk.CardUseFinished then
        return target == player and data.card.type == Card.TypeEquip
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
      return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUseFinished then
      room:notifySkillInvoked(player, self.name, "special")
      if player.maxHp < 6 then
       player:broadcastSkillInvoke("YS__fengkui")
       room:changeMaxHp(player, 1)
      else
        player:broadcastSkillInvoke(self.name)
        room:recover({
          who = player,
          num = 1,
          recoverBy = player,
          skillName = self.name
          })
        room:addPlayerMark(player, "YS__nljianwu-turn", 1)
      end
    end
  end,
}
local YS__nl_Bloom = fk.CreateActiveSkill{
  name = "YS__nl_Bloom",
  prompt = "#YS__nl_Bloom",
  mute = true,
  anim_type = "support",
  card_num = 1,
  target_num = 1,
  can_use = function(self, player)
    return table.find(player:getCardIds("h"), function(id) return Fk:getCardById(id).name == "YS__Seed" end)
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).name == "YS__Seed" and table.contains(Self:getCardIds("h"), to_select)
  end,
  target_filter = function(self, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= Self.id
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    U.presentCard(player, target, Fk:getCardById(effect.cards[1]))
  end,
}
YS__nl:addSkill(YS__fengkui)
YS__nl:addSkill(YS__fulian)
YS__fulian:addRelatedSkill(YS__fulianRecover)
YS__nl:addSkill(YS__jianwu)
YS__nl:addSkill(YS__nl_Bloom)
Fk:loadTranslationTable{
  ["YS__nl"] = "妮露",
  ["YS__fengkui"] = "丰馈",
  [":YS__fengkui"] = "出牌阶段限一次，若你没有【丰壤之核】，则你可以获得一张【丰壤之核】,若你的手牌只有一种颜色则你额外获得一张。"..
  "<br/><font color='grey'>#\"<b>丰壤之核</b>\"：①当【丰壤之核】正面向上离开你的手牌区或作为你的拼点牌亮出后，你失去1点体力并摸一张牌。②当你因摸牌获得【丰壤之核】后，"..
  "你可以将之交给一名其他角色，以此法失去【丰壤之核】时不触发失去体力效果。</font>",
  ["YS__fulian"] = "浮莲",
  [":YS__fulian"] = "锁定技，每当场上有角色流失一点体力时，你摸一张牌。当场上有【丰壤之核】进入弃牌堆后，你可以回复一点体力。",
  ["#YS__fulianRecover"] = "浮莲",
  ["YS__jianwu"] = "剑舞",
  [":YS__jianwu"] = "锁定技，当你使用装备牌后，若你的体力上限小于6，你增加一点体力上限；否则，你回复一点体力且本回合可以额外使用一次〖丰馈〗。",
  ["YS__nl_Bloom"] = "绽放",
  [":YS__nl_Bloom"] = "出牌阶段，你可以从手牌中将一张【丰壤之核】正面向上赠予其他角色。",
  ["#YS__nl_Bloom"] = "将一张【丰壤之核】赠予其他角色",
}

local YS__yjlsD = General(extension, "YS__yjlsD", "Normal", 5, 5, General.Agender)
local YS__shiying = fk.CreateTriggerSkill{
  name = "YS__shiying",
  anim_type = "defensive",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    if not (target == player and player:hasSkill(self)) then return end
    if data.damageType == 1 then
      return math.max(player:getMark("YS_sydmg1"),player:getMark("YS_sydmg2"),player:getMark("YS_sydmg3"),player:getMark("YS_sydmg4")) == player:getMark("YS_sydmg1") and player:getMark("YS_sydmg1") > 2
    elseif data.damageType == 2 then
      return math.max(player:getMark("YS_sydmg1"),player:getMark("YS_sydmg2"),player:getMark("YS_sydmg3"),player:getMark("YS_sydmg4")) == player:getMark("YS_sydmg2") and player:getMark("YS_sydmg2") > 2 
    elseif data.damageType == 3 then
      return math.max(player:getMark("YS_sydmg1"),player:getMark("YS_sydmg2"),player:getMark("YS_sydmg3"),player:getMark("YS_sydmg4")) == player:getMark("YS_sydmg3") and player:getMark("YS_sydmg3") > 2 
    elseif data.damageType == 4 then
      return math.max(player:getMark("YS_sydmg1"),player:getMark("YS_sydmg2"),player:getMark("YS_sydmg3"),player:getMark("YS_sydmg4")) == player:getMark("YS_sydmg4") and player:getMark("YS_sydmg4") > 2
    end
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, self.name)
    data.damage = math.min(0, data.damage - math.floor(player:getMark("YS_sydmg" .. data.damageType)/2))
  end,

  refresh_events = {fk.DamageInflicted},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "YS_sydmg" .. data.damageType, data.damage)
    if player:getMark("YS_sydmg" .. data.damageType) > 6 then
      room:setPlayerMark(player, "YS_sydmg" .. data.damageType, 0)
    end
    room:setPlayerMark(player, "@", string.format("%d普%d<font color='#8600FF'>雷</font>%d<font color='Red'>火</font>%d<font color='#95CACA'>冰</font>",
      player:getMark("YS_sydmg1"),
      player:getMark("YS_sydmg2"),
      player:getMark("YS_sydmg3"),
      player:getMark("YS_sydmg4")
    ))
  end,
}
local YS__bengfa = fk.CreateActiveSkill{
  name = "YS__bengfa",
  anim_type = "offensive",
  prompt = "#YS__bengfa-prompt",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) < 1 and math.max(player:getMark("YS_sydmg1"),player:getMark("YS_sydmg2"),player:getMark("YS_sydmg3"),player:getMark("YS_sydmg4")) > 1
  end,
  interaction = function()
    local choices = {}
    for i = 1, 4 do
      if Self:getMark("YS_sydmg" .. i) > 1 then
        table.insertIfNeed(choices, "YS_BFSH" .. i)
      end
    end
    local allchoices = {
      "YS_BFSH1",
      "YS_BFSH2",
      "YS_BFSH3",
      "YS_BFSH4",
    }
    return UI.ComboBox {
      choices = choices,
      allchoices = allchoices
    }
  end,
  card_filter = function(self, to_select, selected)
    return false
  end,
  target_filter = function(self, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= Self.id
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local c = 0
    local num = 0
    local choice = self.interaction.data
    if choice == "YS_BFSH1" then
      c = 1
      num = math.floor(player:getMark("YS_sydmg1")/2)
    elseif choice == "YS_BFSH2" then
      c = 2
      num = math.floor(player:getMark("YS_sydmg2")/2)
    elseif choice == "YS_BFSH3" then
      c = 3
      num = math.floor(player:getMark("YS_sydmg3")/2)
    elseif choice == "YS_BFSH4" then
      c = 4
      num = math.floor(player:getMark("YS_sydmg4")/2)
    end
    room:damage{
      from = player,
      to = target,
      damage = num,
      damageType = c,
      skillName = self.name,
    }
    room:recover({
      who = player,
      num = num,
      recoverBy = player,
      skillName = self.name
      })
    if choice == "YS_BFSH1" then
      room:setPlayerMark(player, "YS_sydmg1", 0)
    elseif choice == "YS_BFSH2" then
      room:setPlayerMark(player, "YS_sydmg2", 0)
    elseif choice == "YS_BFSH3" then
      room:setPlayerMark(player, "YS_sydmg3", 0)
    elseif choice == "YS_BFSH4" then
      room:setPlayerMark(player, "YS_sydmg4", 0)
    end
    room:setPlayerMark(player, "@", string.format("%d普%d<font color='#8600FF'>雷</font>%d<font color='Red'>火</font>%d<font color='#95CACA'>冰</font>",
    player:getMark("YS_sydmg1"),
    player:getMark("YS_sydmg2"),
    player:getMark("YS_sydmg3"),
    player:getMark("YS_sydmg4")
  ))
  end,
}
YS__yjlsD:addSkill("YS__jixie")
YS__yjlsD:addSkill(YS__shiying)
YS__yjlsD:addSkill(YS__bengfa)
Fk:loadTranslationTable{
  ["YS__yjlsD"] = "遗迹龙兽",
  ["YS__shiying"] = "汲取",
  [":YS__shiying"] = "锁定技，记录你受到的所有伤害类型（普通、火、雷、冰）。对你造成最多伤害的伤害类型对你造成的伤害-X；伤害记录大于6时清空该项记录",
  ["YS_BFSH1"] = "迸发普通伤害",
  ["YS_BFSH2"] = "迸发雷电伤害",
  ["YS_BFSH3"] = "迸发火焰伤害",
  ["YS_BFSH4"] = "迸发冰冻伤害",
  ["YS__bengfa"] = "迸发",
  [":YS__bengfa"] = "出牌阶段限一次，你可以清空一项记录不小于2的伤害类型，对一名其他角色造成X点对应类型伤害；如此做，你回复X点体力。（X为此伤害的记录的一半，向下取整）",
  ["#YS__bengfa-prompt"] = "迸发：清空一项记录对一名角色造成伤害。",
}

return extension